﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Fcom.VNInsurance.Web.Controllers
{
     [Authorize(Roles = "13,14")]
    public class SaveUpToController : Controller
    {
        //
        // GET: /SaveUpTo/
        Models.VNInsuranceDataContext db = new Models.VNInsuranceDataContext();
        public ActionResult Index()
        {

            return View();
        }
        public ActionResult GetJsonSaves()
        {
            var saves = from s in db.SaveUpToLangs where s.LangID == Request.Cookies["_culture"]["lang"] select new { s.DisplayTitle,s.Value,s.LangID,s.Link,s.ID};
            return Json(saves,JsonRequestBehavior.AllowGet);
        }
        public ActionResult EditSave(int ID)
        {
            var saves =(from s in db.SaveUpToLangs where s.LangID == Request.Cookies["_culture"]["lang"]&&s.ID==ID select s).FirstOrDefault();
            return View(saves);
        }
        [HttpPost]
        public ActionResult EditSave(Models.SaveUpToLang model)
        {
            if (ModelState.IsValid)
            {
                var saves = (from s in db.SaveUpToLangs where s.LangID == Request.Cookies["_culture"]["lang"] && s.ID == model.ID select s).FirstOrDefault();
                saves.DisplayTitle = model.DisplayTitle;
                saves.Link = model.Link;
                saves.Value = model.Value;
                db.SubmitChanges();
                return RedirectToAction("Index");
            }
            return View();
        }
        public ActionResult AddSave()
        {
           
            return View();
        }
        [HttpPost]
        public ActionResult AddSave(Models.SaveUpToLang model)
        {
            if (ModelState.IsValid)
            {
                Models.SaveUpTo save = new Models.SaveUpTo();
                save.Title = model.DisplayTitle;
                db.SaveUpTos.InsertOnSubmit(save);
                db.SubmitChanges();
                Models.SaveUpToLang savelang1 = new Models.SaveUpToLang();
                savelang1.DisplayTitle = model.DisplayTitle;
                savelang1.ID = save.Id;
                savelang1.LangID = "en-US";
                savelang1.Link = model.Link;
                savelang1.Value = model.Value;
                db.SaveUpToLangs.InsertOnSubmit(savelang1);

                Models.SaveUpToLang savelang2 = new Models.SaveUpToLang();
                savelang2.DisplayTitle = model.DisplayTitle;
                savelang2.ID = save.Id;
                savelang2.LangID = "vi-VN";
                savelang2.Link = model.Link;
                savelang2.Value = model.Value;
              
                db.SaveUpToLangs.InsertOnSubmit(savelang2);
                db.SubmitChanges();
                return RedirectToAction("Index");
            }
            return View();
        }
    }
}
